<template>
  <el-menu
      :default-active="menu[0].path"
      :collapse="isCollapsed"
      class="el-menu-vertical-demo"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b">
    <h3>{{ isCollapsed ? '后台' : '通用后台管理系统' }}</h3>
    <el-menu-item v-for="item in noChildren" :index="item.path" :key="item.path" @click="choiceItem(item)">
      <i :class="'el-icon-'+item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="(item,index) in hasChildren" :index="index+''" :key="item.path">
      <template slot="title">
        <i :class="'el-icon-'+item.icon"></i>
        <span>{{ item.label }}</span>
      </template>
      <el-menu-item v-for="subItem in item.children" :key="subItem.path" :index="subItem.path"
                    @click="choiceItem(subItem)">
        <i :class="'el-icon-'+subItem.icon"></i>
        <span slot="title">{{ subItem.label }}</span>
      </el-menu-item>
    </el-submenu>
  </el-menu>

</template>

<script>
import {mapState} from 'vuex'

export default {
  name: "CommonAside",
  computed: {
    noChildren() {
      return this.menu.filter(item => !item.children)
    },
    hasChildren() {
      return this.menu.filter(item => item.children)
    },
    ...mapState('tab', ['isCollapsed', 'menu']),

  },
  methods: {
    choiceItem(item) {
      if (item.path !== this.$route.path)
        this.$router.push({
          name: item.name,
        })
      this.$store.commit('tab/selectMenu', item)
    },
  },
}
</script>

<style scoped lang="less">
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  height: 100%;
  border: none;

  h3 {
    color: #ffffff;
    text-align: center;
    white-space: nowrap;
  }
}
</style>
